package view.list.detailview;


import event.Broadcaster;
import event.ChangeDirectoryEvent;
import filesystem.MyFile;
import view.list.PanelChangeRequestListener;

import javax.swing.*;

import javax.swing.table.*;

import java.awt.Rectangle;
import java.awt.event.*;


/**
 * Title:        Java File Manager
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      Home
 *
 * @author Giurgiu Sergiu
 * @version 1.0
 */
public class DetailsTable extends JTable {
    private DetailsTableCellRenderer renderer = new DetailsTableCellRenderer();
    private PanelChangeRequestListener panelChangeListener = null;

    public DetailsTable() {
        super();
        setOtherProperties();
        addMouseActions();
        addKeys();
    }


    public void setPanelChangeRequestListener(PanelChangeRequestListener l) {
        panelChangeListener = l;
    }

    private void addKeys() {
        InputMap mainMap = this.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        mainMap.put(KeyStroke.getKeyStroke("ENTER"), "fileAction"); //MUST ADD an action for this
        this.getActionMap().put("fileAction", new FileAction());
        this.getActionMap().put("upOneDirectory", new UpDirectoryAction());
    }

    private void setOtherProperties() {
        this.getTableHeader().setReorderingAllowed(false);
        this.setShowGrid(false);
        this.setColumnSelectionAllowed(false);
        this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //this.setDragEnabled(true);
    }

    private void addMouseActions() {
        this.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                int row = DetailsTable.this.rowAtPoint(e.getPoint());
                if (e.getClickCount() == 2 && (row >= 0) && (row < DetailsTable.this.getModel().getRowCount())) {
                    //double click on row
                    processActionOnRow(row);
                    return;
                } else if (e.getClickCount() == 1 && (row >= 0) && (row <  DetailsTable.this.getModel().getRowCount())) {
                    //single click on row
                    final DetailsTableModel model = (DetailsTableModel) getModel();
                    final MyFile el = (MyFile) model.getValueAt(row, 0);
                    if (el.isDirectory()) {
                        ChangeDirectoryEvent event = new ChangeDirectoryEvent();
                        event.setDirectory(el);
                        event.setSource(DetailsTable.this);
                        Broadcaster.notifyChangeDirectoryListeners(event);
                    }

                }
            }
        });

    }

    private void processActionOnRow(int row) {
        //this is always gonna be 0
        //if not THEN CHANGE THIS
        final DetailsTableModel model = (DetailsTableModel) getModel();
        int colIndex = 0;//((FullTableViewModel)FullTableView.this.getModel()).getColumnIndex(MyFile.class);
        final MyFile el = (MyFile) model.getValueAt(row, colIndex);
        //6 System.out.println(el.isDirectory());
        if (el.isDirectory()) {
            final MyFile oldworkingDirectory = model.getWorkingDirectory();
            model.browseDirectory(el);
            int index = model.getCurrentFiles().indexOf(oldworkingDirectory);
            if (index >= 0) {
                ensureIndexIsVisible(index);
                getSelectionModel().setSelectionInterval(index, index);
            } else {
                getSelectionModel().setSelectionInterval(0, 0);
            }
        } else {
            /**@todo execute a file*/
        }
    }

    private class FileAction extends AbstractAction {

        public void actionPerformed(ActionEvent e) {
            int row = ((DetailsTable) e.getSource()).getSelectedRow();
            if (row >= 0 && row < DetailsTable.this.getModel().getRowCount()) {
                processActionOnRow(row);
            }
        }
    }

    public TableCellRenderer getCellRenderer(int row, int column) {
        return renderer;
    }


    private class UpDirectoryAction extends AbstractAction {

        public void actionPerformed(ActionEvent e) {
            processActionOnRow(0);
        }
    }

    public void ensureIndexIsVisible(int index) {
        Rectangle cellBounds = this.getCellRect(index, 0, true);
        if (cellBounds != null) {
            scrollRectToVisible(cellBounds);
        }
    }


}
